<?php

/**
 * profile actions.
 *
 * @package    sf_sandbox
 * @subpackage profile
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 12479 2008-10-31 10:54:40Z fabien $
 */
class profileActions extends sfActions
{
 /**
  * Executes index action
  *
  * @param sfRequest $request A request object
  */

     public function executeDel(sfWebRequest $request)
  {
      $user = sfGuardUserPeer::retrieveByPK($request->getParameter('id'));
      if($user->getPartnerId()==$this->getUser()->getProfile()->getId() or $this->getUser()->isSuperAdmin())
          {
            $user->delete();
          }
      $this->redirect('@sf_guard_user');
  }

     public function executeLetter(sfWebRequest $request)
  {
     	$this->u = sfGuardUserPeer::retrieveByPK($request->getParameter('id'));
         if ($request->isMethod('post'))
         {
            $user =  sfGuardUserPeer::retrieveByPK($request->getParameter('id'));
            $users = sfGuardUserPeer::getUsersForGender();
            $i=0;
            foreach($users as $u)
            {
               		        //echo "-".$cnt->getId()."<br>";
                $i++;
				$m=new Mailbox();
				$m->setFromId($user->getId());
				$m->setToId($u->getId());
                                $m->setFree(true);
                                $m->setToPartnerId($u->getPartnerId());
                                $m->setFromPartnerId($user->getPartnerId());
				$m->setBody($request->getParameter('message'));
				$m->setPub(1);
				$m->save();

				//// Шлем уведомление
				$not = MailnoticePeer::retrieveByPK ( 3 );
				$title = $not->getTitle ();
                                
				$body = $not->parse_body ( array ('%from%' => $user->getUsername(), '%link%' => "<a href='" . sfConfig::get ( 'app_site_url' ) . "'>" . __ ( 'ссылку' ) . "</a>" ) );
				myTools::sendMail ( $u->getEmail(), $title, $body );
            }
            $this->getUser()->setFlash('notice', __('Отправлено').' <b>'.$i.'</b> '.__('писем'));
         }
  }

	
  public function executeEdit(sfWebRequest $request)
  {
  	$strform ='adminProfileForm';	

        if($request->getParameter('id')>0)
        {
          $pf = sfGuardUserPeer::retrieveByPK($request->getParameter('id'));
          if($pf->getGender()=='m')
              {
                 $pf->setPartnerId(2);
              }
        }
        else
        {
           $pf = new sfGuardUser();
           $pf->setPartnerId($this->getUser()->getAttribute( 'user_id', null, 'sfGuardSecurityUser' ));
        }
        $this->form = new $strform($pf);

   if ($request->isMethod('post'))
    {
    	$pf = sfGuardUserPeer::retrieveByPK($request->getParameter('user[id]'));
        

        $this->form = new $strform($pf);
    	$this->form->bind($request->getParameter('user'), $request->getFiles('user'));
    if ($this->form->isValid())
      {
      	
      	        $this->getUser ()->setFlash ( 'notice', 'Данные сохранены.' );
         	$this->form->save();
         	$pf = $this->form->getObject();


                $name = $pf->getFirstName().$pf->getId();

                $pf->setUsername($name);

         	////Сохраняем мултики
            
            if($request->getParameter('user[cmy_read][]'))
            {
            	$arr = array();
            	$arr = $request->getParameter('user[cmy_read]');
            	$str = myTools::db_array_field($arr);
            	$pf->setMyRead($str);
            }
            if($request->getParameter('user[cmy_musik][]'))
            {
            	$arr = array();
            	$arr = $request->getParameter('user[cmy_musik]');
            	$str = myTools::db_array_field($arr);
            	$pf->setMyMusik($str);

            }        	
            if($request->getParameter('user[cmy_rest][]'))
            {
            	$arr = array();
            	$arr = $request->getParameter('user[cmy_rest]');
            	$str = myTools::db_array_field($arr);
            	$pf->setMyRest($str);
            }   
            if($request->getParameter('user[cmy_harakter][]'))
            {
            	$arr = $request->getParameter('user[cmy_harakter]');
            	$str = myTools::db_array_field($arr);
            	$pf->setMyHarakter($str);
            }  
            if($request->getParameter('user[cmy_food][]'))
            {
            	$arr = $request->getParameter('user[cmy_food]');
            	$str = myTools::db_array_field($arr);
            	$pf->setMyFood($str);
            }
            if($request->getParameter('user[cmy_sport][]'))
            {
            	$arr = $request->getParameter('user[cmy_sport]');
            	$str = myTools::db_array_field($arr);
            	$pf->setMySport($str);
            }            
            $pf->save();
         	///
             $this->redirect('profile/edit?id='.$this->form->getObject()->getId());
      }
      else 
      {
      	   $this->getUser ()->setFlash ( 'error', 'Возникла ошибка при сохранении.' );
      	  
      }
      
    }
    

    
  }
  
  

   public function executeAdd_child(sfWebRequest $request)
  {
  	$this->form = new ChildForm();
     if ($request->isMethod('post'))
    {
    	//$pf = sfGuardUserPeer::retrieveByPK($this->getUser()->getAttribute( 'user_id', null, 'sfGuardSecurityUser' ));
    	$this->form->bind($request->getParameter('child'), $request->getFiles('child'));
    if ($this->form->isValid())
      {
      	    $this->getUser ()->setFlash ( 'notice', 'Данные сохранены.' );
         	$this->form->save();
         	$this->redirect('@edit_profile?id='.$this->form->getObject()->getsfGuardUser()->getId());
      }
    }
    ///Кидаем пользователя в layout
	     $this->getRequest()->setAttribute('v', $this->getUser()->getProfile());
	////
  }
  
  public function executeDel_child(sfWebRequest $request)
  {
  	 $u2l = ChildPeer::retrieveByPK($request->getParameter('id'));
  	 $u2l->delete();
  	 $this->redirect('@edit_profile?id='.$this->getUser()->getAttribute( 'user_id', null, 'sfGuardSecurityUser' ));
  }
  
  
  
   public function executeAdd_language(sfWebRequest $request)
  {
  	$u2l = new User2lang();
  	$u2l->setUserId($request->getParameter('id'));
  	$this->form = new User2langForm($u2l);
     if ($request->isMethod('post'))
    {
    	//$pf = sfGuardUserPeer::retrieveByPK($this->getUser()->getAttribute( 'user_id', null, 'sfGuardSecurityUser' ));
    	$this->form->bind($request->getParameter('user2lang'));
    if ($this->form->isValid())
      {
      	    $this->getUser ()->setFlash ( 'notice', 'Данные сохранены.' );
         	$this->form->save();
         	$this->redirect('@edit_profile?id='.$this->form->getObject()->getsfGuardUser()->getId());
      }
    }
  		 ///Кидаем пользователя в layout
	    $this->getRequest()->setAttribute('v', $this->getUser()->getProfile());
		////    
  }
  
  public function executeDel_language(sfWebRequest $request)
  {
  	 $u2l = User2langPeer::retrieveByPK($request->getParameter('id'));
  	 $u2l->delete();
  	 $this->redirect('@edit_profile?id='.$this->getUser()->getAttribute( 'user_id', null, 'sfGuardSecurityUser' ));
  }
  
    public function executeVideo(sfWebRequest $request)
  {
  	 $this->u = sfGuardUserPeer::retrieveByPK($request->getParameter('id'));
         $v = new Video();
         $v->setUserId($this->u->getId());
  	 $this->form = new profileVideoForm($v);
             if ($request->isMethod('post'))
            {
              $this->form->bind($request->getParameter('video'), $request->getFiles('video'));
              if ($this->form->isValid())
              {
                $this->form->save();
                $this->getUser()->setFlash('notice', __('Данные сохранены'));
                $this->redirect('profile/video?id='.$this->form->getObject()->getUserId());
              }
            }
         
  }
  
  
  
    public function executePhoto(sfWebRequest $request)
  {
  	 $this->u = sfGuardUserPeer::retrieveByPK($request->getParameter('id'));
         $v = new Gallery();
         $v->setUserId($this->u->getId());
  	     $this->form = new profilePhotoForm($v);
             if ($request->isMethod('post'))
            {
              $this->form->bind($request->getParameter('gallery'), $request->getFiles('gallery'));
              if ($this->form->isValid())
              {
                $this->form->save();
                $this->getUser()->setFlash('notice', __('Данные сохранены'));
                $this->redirect('profile/photo?id='.$this->form->getObject()->getUserId());
              }
            }
         
  }
    
  

    public function executeShow_video(sfWebRequest $request)
  {
     $this->v = VideoPeer::retrieveByPK($request->getParameter('id'));
     $this->setLayout('video_layout');
  }

      public function executeDel_video(sfWebRequest $request)
  {
     $v = VideoPeer::retrieveByPK($request->getParameter('id'));
    if($v) {
        $user_id = $v->getUserId();
        $v->delete();
    }
    $this->redirect('profile/video?id='.$user_id);
  }
  
      public function executeDel_photo(sfWebRequest $request)
  {
     $v = GalleryPeer::retrieveByPK($request->getParameter('id'));
    if($v) {
        $user_id = $v->getUserId();
        $v->delete();
    }
    $this->redirect('profile/photo?id='.$user_id);
  }  
  
  
}
